<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="12" :sm="8">
            <a-form-item label="用户名称">
              <a-input placeholder="请输入用户名称" v-model="queryParam.categoryName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-if="roleId == null">新增</a-button>
      <a-button @click="handleAddUserRole" type="primary" icon="plus" style="margin-top: 16px" v-if="roleId">
        已有用户
      </a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <CustomColList :propsDefColumns.sync="columns" @refresh="loadData" />
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
        }"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->
    <UserModal ref="modalForm" @ok="modalFormOk" />
    <Select-User-Modal ref="selectUserModal" @selectFinished="selectOK"></Select-User-Modal>
  </a-card>
</template>

<script>
import { getAction, postAction } from '@/api/basic';
import { ListMixin } from '../../mixins/ListMixin';
import CustomColList from '@/components/custom/CustomColList';
import UserModal from './modules/UserModal';
import SelectUserModal from './modules/SelectUserModal';
export default {
  name: 'UserList',
  mixins: [ListMixin],
  components: {
    CustomColList,
    UserModal,
    SelectUserModal,
  },
  props: {
    roleId: {
      type: Number,
      default: null,
    },
  },
  data() {
    return {
      description: '用户列表',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          },
        },
        {
          title: '用户名称',
          align: 'center',
          dataIndex: 'username',
        },
        {
          title: '角色名称',
          align: 'center',
          dataIndex: 'role.roleName',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/users/list',
        delete: '/users/delete',
        bindRole: '/users/bindRole',
      },
    };
  },
  mounted() {},
  watch: {
    roleId: function (val) {
      if (val) {
        this.queryParam.roleId = val;
        this.loadData();
      }
    },
  },
  computed: {},
  methods: {
    handleAddUserRole() {
      if (this.currentRoleId == '') {
        this.$message.error('请选择一个角色!');
      } else {
        this.$refs.selectUserModal.visible = true;
      }
    },
    selectOK(data) {
      let params = {};
      params.roleId = this.roleId;
      params.userIdList = [];
      for (var a = 0; a < data.length; a++) {
        params.userIdList.push(data[a]);
      }
      console.log(params);
      postAction(this.url.bindRole, params).then((res) => {
        if (res.success) {
          this.loadData();
          this.$message.success(res.message);
        } else {
          this.$message.warning(res.message);
        }
      });
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!');
        return;
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = this.getQueryParams(); //查询条件
      console.log(`this.roleId`, this.roleId);
      if (this.roleId) {
        params.roleId = this.roleId;
      }
      console.log(`params`, params);
      this.loading = true;
      getAction(this.url.list, params)
        .then((res) => {
          if (res.success) {
            this.dataSource = res.result.records;
            this.ipagination.total = res.result.total;
          }
          if (res.code === 510) {
            this.$message.warning(res.message);
          }
          this.loading = false;
        })
        .catch((res) => {
          // this.$message.error(res.message)
          this.loading = false;
        });
    },
  },
};
</script>
<style scoped></style>
